package com.fzdy.dataplatform.dao.impl;


import com.fzdy.dataplatform.dao.AppDao;
import com.fzdy.dataplatform.entity.App;
import org.hibernate.Criteria;
import org.hibernate.criterion.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class AppDaoImpl extends PseudoDelete<App> implements AppDao {

    @Override
    public List<App> findByExampleLike(App example, int pageNo, int pageSize) {
        Criteria criteria = this.getSession().createCriteria(this.clazz).add(Example.create(example).enableLike(MatchMode.ANYWHERE));
        if (example.getUser() != null){
            criteria.createAlias("user","user");//添加外键关联。
            criteria.add(Restrictions.like("user.name",example.getUser().getName(), MatchMode.ANYWHERE));
        }
        return this.createPageOrder(criteria, pageNo, pageSize, Order.desc("id")).list();
    }

    @Override
    public long countByExampleLike(App example) {
        Criteria criteria = this.getSession().createCriteria(this.clazz).add(Example.create(example).enableLike(MatchMode.ANYWHERE));
        if (example.getUser() != null){
            criteria.createAlias("user","user");//添加外键关联。
            criteria.add(Restrictions.like("user.name",example.getUser().getName()));
        }
        Long o = (Long) criteria.setProjection(Projections.count("id")).uniqueResult();
        return o.longValue();
    }


}
